﻿using DeviceMaintainGIS.Server.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace DeviceMaintainGIS.Server.Controllers
{
    public class MessageController : Controller
    {
        /// <summary>
        /// 添加信息到SignalR
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        // GET: Message
        [HttpPost]
        public string MessageAdd(MessageModel mm)
        {
            try
            {
                JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
                String strJson = jsonSerialize.Serialize(mm);//输出结果：{"Id":1,"UserName":"张三","Time":"\/Date(1297950721668)\/"}
                string pars = string.Format("添加广播信息：\r\n 时间 = {0};\r\n 参数 = {1};"
                , System.DateTime.Now
                , strJson);
                LogHelper.WriteLog(typeof(MessageController), pars);
                ChatHub.MessageList.Add(mm);
            }
            catch(Exception ee)
            {
                string pars = string.Format("添加广播信息报错信息：\r\n 时间 = {0};\r\n 参数 = {1};"
                , System.DateTime.Now
                , ee.Message);
                LogHelper.WriteLog(typeof(MessageController), pars);
                return ee.Message;
            }
            return "";
        }
        /// <summary>
        /// 删除推送信息
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        public string MessageDel(MessageModel mm)
        {
            try
            {
                JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
                String strJson = jsonSerialize.Serialize(mm);//输出结果：{"Id":1,"UserName":"张三","Time":"\/Date(1297950721668)\/"}
                string pars = string.Format("删除广播信息：\r\n 时间 = {0};\r\n 参数 = {1};"
                , System.DateTime.Now
                , strJson);
                LogHelper.WriteLog(typeof(MessageController), pars);
                MessageModel tmp = ChatHub.MessageList.Where(i => i.UserID == mm.UserID && i.Message == mm.Message).FirstOrDefault();
                ChatHub.MessageList.Remove(tmp);
            }
            catch (Exception ee)
            {
                string pars = string.Format("删除广播信息报错信息：\r\n 时间 = {0};\r\n 参数 = {1};"
                , System.DateTime.Now
                , ee.Message);
                LogHelper.WriteLog(typeof(MessageController), pars);
                return ee.Message;
            }
            return "";
        }
        public ActionResult Index()
        {
            
            return View();
        }
    }
}